/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GEP.po;

import GEP.params.Params;
import com.oreilly.servlet.MultipartRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author mg
 */
public class JobInfo {

    String project = "";
    String email = "";
    String input = "";
    final String output;
    final String error;
    String cpu = "";
    String arch = "";
    String queue = "";
    String strPE = "";
    String strOthers = "";

    //Members whose values are derived from the above
    final String projectDirectory;
    String jobName = "";
    String appName = "";
    String binary = "";
    String xApp = "";
    ArrayList envVars = new ArrayList();
    String parallel = "";

    //Private members used for deriving the above
    private BufferedReader projectInfo;
    private BufferedReader appInfo;

    //==============================================================================
    /**
     * Constructor
     * @param request HttpServletRequest
     * @param home String
     * @param uid String
     * @throws FileNotFoundException
     * @throws IOException
     */
    //==============================================================================
    public JobInfo(HttpServletRequest request, String home, String uid, MultipartRequest multi) throws
            FileNotFoundException, IOException {

        project = multi.getParameter("project");
        email = multi.getParameter("email");
        input = multi.getParameter("input");
        output = multi.getParameter("output");
        error = multi.getParameter("error");
        cpu = multi.getParameter("cpu");
        arch = multi.getParameter("arch"); //added to get architecture.
        queue = multi.getParameter("queue"); //added to get queuename
        strPE = multi.getParameter("pe");
        strOthers = multi.getParameter("others");

        //----------------------------------------------------------------------
        //Compute derived values
        //----------------------------------------------------------------------
        projectDirectory = home + project;
        projectInfo = new BufferedReader(new FileReader(new File(
                projectDirectory,
                Params.SUNTCP_PROJECT)));
        jobName = projectInfo.readLine().replace(' ', '_').replace('\'', '_');
        appName = projectInfo.readLine();

        //----------------------------------------------------------------------
        //ONLY ACCEPTS PARALLEL JOB IF IT IS SETTED
        //----------------------------------------------------------------------
        appInfo = new BufferedReader(new FileReader(new File(Params.APP_HOME_DIR +
                appName, Params.SUNTCP_APP)));
        appInfo.readLine();
        binary = Params.APP_HOME_DIR + appName + "/" + appInfo.readLine();
        parallel = appInfo.readLine();
        xApp = appInfo.readLine();

        //----------------------------------------------------------------------
        // Build ArrayList of environment variables from application- and
        // project-specific info.
        //----------------------------------------------------------------------
        String line;
        envVars = new ArrayList();
        appInfo.readLine();
        appInfo.readLine();
        while ((line = appInfo.readLine()) != null) {
            envVars.add(line);
        }
        appInfo.close();
        while ((line = projectInfo.readLine()) != null) {
            envVars.add(line);
        }
        projectInfo.close();
    }
}
